package com.example.vv.model.main.base.presenter;

import android.app.Activity;
import android.widget.Toast;

import com.example.vv.frameWork.entity.LoginEntity;
import com.example.vv.frameWork.mvp.MvpPresenter;
import com.example.vv.frameWork.mvp.ViewISNullException;
import com.example.vv.frameWork.sp.Userinfo;
import com.example.vv.frameWork.until.SharePUntitl;
import com.example.vv.model.main.base.contract.MainContract;
import com.example.vv.model.main.base.model.MainModel;
import com.example.vv.model.splash.base.contract.SplashContract;
import com.example.vv.req.REQ_LOGIN;
import com.example.vv.res.BaseRespont;
import com.google.gson.Gson;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class MainPresenter extends MvpPresenter<MainContract.View, MainContract.Model> implements MainContract.Presenter {
    @Override
    public void attach(MainContract.View view) {
        super.attach(view);
        model = new MainModel();
    }

    public void initData() {
        try {
            Userinfo userBean = SharePUntitl.getUserBean((Activity) view().getMContext());
            if (userBean != null) {
                model.login(new REQ_LOGIN(userBean.getPhone(), userBean.getPassWord()))
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .map(new Function<BaseRespont, LoginEntity>() {
                            @Override
                            public LoginEntity apply(BaseRespont baseRespont) throws Exception {
                                return new Gson().fromJson(baseRespont.getBaseData(), LoginEntity.class);
                            }
                        })
                        .subscribe(new Observer<LoginEntity>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(LoginEntity baseRespont) {
                                try {
                                    SharePUntitl.addUserInfo((Activity) view().getMContext(), baseRespont);
                                } catch (ViewISNullException e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onError(Throwable e) {
                                try {
                                    Toast.makeText(view().getMContext(), e.toString(), Toast.LENGTH_LONG).show();
                                } catch (ViewISNullException ex) {
                                    ex.printStackTrace();
                                }
                            }

                            @Override
                            public void onComplete() {

                            }
                        });
            }
        } catch (ViewISNullException e) {
            e.printStackTrace();
        }

    }
}
